<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    <meta name="format-detection" content="address=no" />
    <meta name="format-detection" content="date=no" />
    <title></title>
    <link href="./css/index.css" rel="stylesheet" type="text/css">
    <link href="./css/public_win.css" rel="stylesheet" type="text/css">
    <link href="./css/public.css" rel="stylesheet" type="text/css">
</head>

<body>

</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/vue.min.js"></script>
<script type="text/javascript" src="./script/service/service-app.js"></script>
<script type="text/javascript" src="./script/service/service-guide.js"></script>
<script type="text/javascript" src="./script/service/service-user.js"></script>
<script type="text/javascript" src="./script/service/service-storage.js"></script>
<script type="text/javascript" src="./script/service/service-logic.js"></script>
<script type="text/javascript" src="./script/service/service-im.js"></script>
<script type="text/javascript" src="./script/service/service-pic.js"></script>
<script type="text/javascript" src="./script/service/service-tool.js"></script>
<script type="text/javascript" src="./script/localFile.js"></script>
<script type="text/javascript">
    var USER;
    var $S;
    apiready = function () {
        // 因为华为审核问题 这里的请求权限问题 调整到了 fnInitPage 函数中
        // var resultList = api.hasPermission({
        //     list: ['storage', 'notification']
        // });
        // if (!resultList[0].granted) {
        //     api.requestPermission({
        //         list: ['storage']
        //     }, function(ret) {
        //         if (!ret.list[0].granted && api.systemType != 'ios') {
        //             fnToast('您已禁止了应用的存储权限，会影响您的使用体验')
        //         }
        //         setTimeout(function() {
        //             if (!resultList[1].granted) {
        //                 api.requestPermission({
        //                     list: ['notification']
        //                 }, function(ret) {
        //                     // alert(JSON.stringify(ret)+'==requestPermission');
        //                     if (!ret.list[0].granted && api.systemType != 'ios') {
        //                         fnToast('您已禁止了应用进行推送，可能会错过重要通知')
        //                     }
        //                 });
        //             }
        //         }, 1000)
        //     });
        // } else {
        //     setTimeout(function() {
        //         if (!resultList[1].granted) {
        //             api.requestPermission({
        //                 list: ['notification']
        //             }, function(ret) {
        //                 // alert(JSON.stringify(ret)+'==requestPermission');
        //                 if (!ret.list[0].granted && api.systemType != 'ios') {
        //                     fnToast('您已禁止了应用进行推送，可能会错过重要通知')
        //                 }
        //             });
        //         }
        //     }, 1000)
        // }
        $S = SERVICE();
        USER = USER();
        localStorage.safeTop = api.safeArea.top; //获取顶部状态栏的高度
        localStorage.safeBottom = api.safeArea.bottom; //获取底部栏的高度
        localStorage.loadShopUrl = '0';
        api.setScreenOrientation({
            orientation: 'portrait_up'
        });
        api.setPrefs({
            key: 'safeTop',
            value: api.safeArea.top
        });
        api.setPrefs({
            key: 'safeBottom',
            value: api.safeArea.bottom
        });
        api.setPrefs({
            key: 'first_reboot',
            value: '1'
        });
        fixStatusBarStyle(); //设置状态栏样式
        hasProtocol(); //页面初始化
        // 初始化下载
        initLocalDownload();
        // 加入下载
        api.addEventListener({
            name: 'course_insert_download'
        }, function (ret) {
            var data = ret.value.data, callback = ret.value.callback,
                downloadKey = ret.value.downloadKey;
            insertDownload(data, callback, downloadKey);
        });
        // 取消正在下载
        api.addEventListener({
            name: 'cancle_downloading'
        }, function (ret) {
            fnCancleDownloading();
        });
        // 是否排队中
        api.addEventListener({
            name: 'course_is_download'
        }, function (ret) {
            var id = ret.value.id;
            var index = menuIsDownload(id);
            if (index && index != -1) {
                api.sendEvent({
                    name: ret.value.callback,
                    extra: {
                        status: 1
                    }
                });
            }
        })
        // 断网提示
        if (api.connectionType == 'none') {
            alert('当前网络不可用，请检查您的网络设置');
        } else {
            fnRegisterVistor();
        }
    };

    function fnRegisterVistor() {
        var url = 'apiTbAppUser/visitorRegistered';
        var sysStr = api.systemType + ' ' + api.systemVersion;
        var devoiceStr = api.deviceModel;
        var params = {
            termDetails: sysStr + ',' + devoiceStr
        };
        POST(url, params, function () { });
    }

    var tGuide;

    function fnInitPage() {
        //检查程序是否第一次启动（一期用作登录页面的显示，二期作为引导页显示判断）
        // 正式启动，打开首页

        // 检查程序是否第一次启动（引导页显示判断）
        // if (fnIsFirstLaunch()) {
        //     //初始化引导页对象
        //     tGuide = GUIDE(function() {
        //         fnSetLaunchedStatus();
        //         fnOpenMain();
        //         setTimeout(function() {
        //             tGuide.Close();
        //         }, 1000);
        //         //第一次下载进入APP时引导用户登录
        //         setTimeout(function() {
        //             fnOpenLogin();
        //         }, "1500");
        //     });
        //     //显示引导页
        //     tGuide.Show();
        // } else {
        //     openMainWin();
        // }



        var resultList = api.hasPermission({
            list: ['storage', 'notification']
        });
        if (!resultList[0].granted) {
            api.requestPermission({
                list: ['storage']
            }, function (ret) {
                if (!ret.list[0].granted && api.systemType != 'ios') {
                    fnToast('您已禁止了应用的存储权限，会影响您的使用体验')
                }
                setTimeout(function () {
                    if (!resultList[1].granted) {
                        api.requestPermission({
                            list: ['notification']
                        }, function (ret) {
                            // alert(JSON.stringify(ret)+'==requestPermission');
                            if (!ret.list[0].granted && api.systemType != 'ios') {
                                fnToast('您已禁止了应用进行推送，可能会错过重要通知')
                            }
                        });
                    }
                }, 1000)
            });
        } else {
            setTimeout(function () {
                if (!resultList[1].granted) {
                    api.requestPermission({
                        list: ['notification']
                    }, function (ret) {
                        // alert(JSON.stringify(ret)+'==requestPermission');
                        if (!ret.list[0].granted && api.systemType != 'ios') {
                            fnToast('您已禁止了应用进行推送，可能会错过重要通知')
                        }
                    });
                }
            }, 1000)
        }

        // umAnalytics插件更新后 新增了config函数 需要在同意隐私协议后配置
        var umAnalytics = api.require('umAnalytics');
        umAnalytics.config(function (ret) {
            console.log(`umAnalytics ${JSON.stringify(ret)}`);
        });


        openMainWin();
    }

    function hasProtocol() {
        fnDownloadVadBin();
        var isAgree = api.getPrefs({
            sync: true,
            key: '_user_is_agree_'
        });
        if (isAgree != 'userAgree') {
            openPersionWarning();
            api.addEventListener({
                name: '_user_is_agree_'
            }, function () {
                fnInitPage();
            })
        } else {
            fnInitPage();
        }
    }

    function fnDownloadVadBin() {
        var fs = api.require('fs');
        fs.exist({
            path: 'fs://vad.bin'
        }, function (ret, err) {
            if (ret.exist) {
                console.log('声通vad.bin存在');
            } else {
                console.error('声通vad.bin不存在,去下载');
                var url = 'http://langwen.oss-cn-beijing.aliyuncs.com/banckground/3a9233624e194c06b3fa2e0c93e75195.bin';
                api.download({
                    url: url,
                    savePath: 'fs://vad.bin',
                    report: true,
                    cache: true,
                    allowResume: true
                }, function (ret, err) {
                    if (ret.state == 1) {
                        //下载成功
                        console.log('声通bin下载成功');
                    } else if (ret.state == 2) {
                        console.error('声通bin下载失败');
                        setTimeout(function () {
                            fnDownloadVadBin();
                        }, 2 * 1000);
                    }
                });
            }
        });
    }

    // 隐私协议
    function openPersionWarning() {
        var params = {
            name: 'user_info_warning',
            url: './html/agreement.html',
            rect: {
                x: 0, y: 0, w: 'auto', h: 'auto'
            },
            bgColor: 'rgba(0,0,0,0)'
        }
        fnOpenFrame(params);
    }

    var data = {
        '1': {
            'rightBtn': 'share',
            'templateNo': '001',
            'templateType': 'list'
        },
        '2': {
            'rightBtn': 'share',
            'templateNo': '002',
            'templateType': 'get'
        },
        '0': {
            'templateNo': '001',
            'templateType': 'list'
        },
    }
    function handle(index) {
        var item = data[index];
        openCommon(item, item.rightBtn);
    }
</script>

</html>